package com.sun.mybatisplus.elasticsearch.api;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.sun.mybatisplus.elasticsearch.model.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Auth: syh
 * @Date: 2021/10/25 13:05
 * @Email: 1014170249@qq.com
 * @Desc: 不在能知，而在能行
 */
@RestController
public class EsApi {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    /**
     * 创建索引
     *
     * @return
     */
    @RequestMapping("/index")
    public String createIndex() {
        CreateIndexResponse response = null;
        boolean acknowledged = false;
        try {
            response = restHighLevelClient.indices().create(new CreateIndexRequest("user"), RequestOptions.DEFAULT);
            acknowledged = response.isAcknowledged();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "创建索引：" + acknowledged;
    }


    /**
     * 查看索引信息
     *
     * @return
     */
    @RequestMapping("/getIndex")
    public Map<String, List<AliasMetadata>> getIndex() {
        GetIndexResponse userIndex = null;
        Map<String, List<AliasMetadata>> aliases = null;
        try {
            userIndex = restHighLevelClient.indices().get(new GetIndexRequest("user"), RequestOptions.DEFAULT);
            aliases = userIndex.getAliases();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return aliases;
    }


    /**
     * 删除索引
     *
     * @return
     */
    @RequestMapping("/delete")
    public String deleteIndex() {
        AcknowledgedResponse response = null;
        try {
            response = restHighLevelClient.indices().delete(new DeleteIndexRequest("user"), RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean acknowledged = response.isAcknowledged();
        return "删除索引：" + acknowledged;
    }



    /**
     * 向索引中添加文档信息，新增一条数据
     *
     * @return
     */
    @RequestMapping("/add")
    public String createDoc() {
        IndexRequest request = new IndexRequest();
        request.index("user").id("1002");
        //es中的操作都是基于json格式进行操作的
        User user = new User();
        user.setName("zhangsan").setSex("男").setAge(34);
        //转成JSON格式数据
        String userJson = JSONUtil.toJsonStr(user);
        request.source(userJson, XContentType.JSON);
        IndexResponse response = null;
        try {
             response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response.getId();
    }
}
